package com.gable.report.controller;

import com.gable.report.common.util.ReturnResultUtil;
import com.gable.report.model.ChartsHospitalQuery;
import com.gable.report.model.User;
import com.gable.report.service.ChartsForHospitalBaseDataService;
import com.gable.report.util.ChartsUtil;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * Created by lvdouStart on 2017/7/20.
 * 区报表某设备类型的年限指数Controller层
 *
 */
@Controller
@RequestMapping("charts/hospital/model-index")
public class ChartsForHospitalDeviceModelIndexController {

    private final Log logger = LogFactory.getLog(this.getClass());

    @Resource
    HttpServletRequest request;

    @Autowired
    ChartsForHospitalBaseDataService chartsForHospitalBaseDataService;

    @RequestMapping(value="/page", method= RequestMethod.GET)
    public String page(ChartsHospitalQuery chartsHospitalQuery){
        User user = (User)request.getSession().getAttribute("loginUser");
        return ChartsUtil.checkAuthority(user,chartsHospitalQuery) ? "charts/hospital/model-index" : "charts-authority";
    }

    /** 
     * @Author: 杨青桦
     * @Description:  主页总览概览框接口
     * @Date: 2017/7/19 16:04
     * @param chartsHospitalQuery
     * @return
     */
    @RequestMapping(value="/overview",method= RequestMethod.POST,produces="application/json;charset=utf-8")
    @ResponseBody
    public ReturnResultUtil queryAllAssets(ChartsHospitalQuery chartsHospitalQuery){
        User user = (User)request.getSession().getAttribute("loginUser");
        Long hospitalId = ChartsUtil.getHospitalId(user,chartsHospitalQuery);
        Map<String,Object> map;
        try {
            map = chartsForHospitalBaseDataService.queryAllAssetsForDeviceAgeLimit(hospitalId, chartsHospitalQuery);
        }catch (Exception e){
            e.printStackTrace();
            return new ReturnResultUtil(0,"查询异常");
        }
        return new ReturnResultUtil(1,"查询成功",map);
    }

    /** 
     * @Author: 杨青桦
     * @Description:  主维度 -- 图表
     * @Date: 2017/7/19 16:30
     * @param chartsHospitalQuery
     * @return
     */
    @RequestMapping(value="/department",method= RequestMethod.POST,produces="application/json;charset=utf-8")
    @ResponseBody
    public ReturnResultUtil queryDepartments(ChartsHospitalQuery chartsHospitalQuery){
        User user = (User)request.getSession().getAttribute("loginUser");
        Long hospitalId = ChartsUtil.getHospitalId(user,chartsHospitalQuery);
        Map<String,Object> map;
        try {
            map = chartsForHospitalBaseDataService.queryDepartments(hospitalId, chartsHospitalQuery);
        }catch (Exception e){
            e.printStackTrace();
            return new ReturnResultUtil(0,"查询异常");
        }
        return new ReturnResultUtil(1,"查询成功",map);
    }

    /** 
     * @Author: 杨青桦
     * @Description:  设备分类和型号
     * @Date: 2017/7/19 16:30
     * @param chartsHospitalQuery
     * @return
     */
    @RequestMapping(value="/model",method= RequestMethod.POST,produces="application/json;charset=utf-8")
    @ResponseBody
    public ReturnResultUtil queryDeviceModels(ChartsHospitalQuery chartsHospitalQuery){
        User user = (User)request.getSession().getAttribute("loginUser");
        Long hospitalId = ChartsUtil.getHospitalId(user,chartsHospitalQuery);
        Map<String,Object> map;
        try {
            map = chartsForHospitalBaseDataService.queryDeviceModels(hospitalId, chartsHospitalQuery);
        }catch (Exception e){
            e.printStackTrace();
            return new ReturnResultUtil(0,"查询异常");
        }
        return new ReturnResultUtil(1,"查询成功",map);
    }


    /** 
     * @Author: 杨青桦
     * @Description: “➕”具体设备信息
     * @Date: 2017/7/20 11:42
     * @param chartsHospitalQuery
     * @return
     */
    @RequestMapping(value="/device",method= RequestMethod.POST,produces="application/json;charset=utf-8")
    @ResponseBody
    public ReturnResultUtil queryDeviceInfo(ChartsHospitalQuery chartsHospitalQuery){
        User user = (User)request.getSession().getAttribute("loginUser");
        Long hospitalId = ChartsUtil.getHospitalId(user,chartsHospitalQuery);
        Map<String,Object> map;
        try {
            map = chartsForHospitalBaseDataService.queryDeviceInfoByModelName(hospitalId, chartsHospitalQuery);
        }catch (Exception e){
            e.printStackTrace();
            return new ReturnResultUtil(0,"查询异常");
        }
        return new ReturnResultUtil(1,"查询成功",map);
    }

    /**
     * @Author: 杨青桦
     * @Description: 某设备类型的厂家分布
     * @Date: 2017/7/20 11:42
     * @param chartsHospitalQuery
     * @return
     */
    @RequestMapping(value="/brand",method= RequestMethod.POST,produces="application/json;charset=utf-8")
    @ResponseBody
    public ReturnResultUtil queryDeviceBrands(ChartsHospitalQuery chartsHospitalQuery){
        User user = (User)request.getSession().getAttribute("loginUser");
        Long hospitalId = ChartsUtil.getHospitalId(user,chartsHospitalQuery);
        Map<String,Object> map;
        try {
            map = chartsForHospitalBaseDataService.queryDeviceBrands(hospitalId, chartsHospitalQuery);
        }catch (Exception e){
            e.printStackTrace();
            return new ReturnResultUtil(0,"查询异常");
        }
        return new ReturnResultUtil(1,"查询成功",map);
    }


    /**
     * @Author: 杨青桦
     * @Description: 所有具体设备详细信息接口
     * @Date: 2017/9/6 14:14
     * @param chartsHospitalQuery
     * @return
     */
    @RequestMapping(value="/assets",method= RequestMethod.POST,produces="application/json;charset=utf-8")
    @ResponseBody
    public ReturnResultUtil queryAllDevices(ChartsHospitalQuery chartsHospitalQuery){
        User user = (User)request.getSession().getAttribute("loginUser");
        Long hospitalId = ChartsUtil.getHospitalId(user,chartsHospitalQuery);
        Map<String,Object> map;
        try {
            map = chartsForHospitalBaseDataService.queryDeviceInfoByModelName(hospitalId, chartsHospitalQuery);
        }catch (Exception e){
            e.printStackTrace();
            return new ReturnResultUtil(0,"查询异常");
        }
        return new ReturnResultUtil(1,"查询成功",map);
    }

}
